package com.zzb.weikeautosign.vo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 朋友圈VO
 * Created by zzb on 16/2/28.
 */
public class UserShareVo {
    private JSONObject json;
    private JSONArray userShareList;

    public UserShareVo(JSONObject json) {
        this.json = json;
        this.userShareList = json.getJSONArray("userShareList");
    }

    /**
     * 获取id列表
     *
     * @return
     */
    public List<String> getIds() {
        List<String> ids = new ArrayList<>();
        if (userShareList != null) {
            for (int i = 0; i < userShareList.size(); i++) {
                JSONObject item = userShareList.getJSONObject(i);
                String id = item.getString("id");
                if (StringUtils.isNotBlank(id)) {
                    ids.add(id);
                }
            }
        }
        return ids;
    }

    /**
     * 筛选没有点赞的id
     * @param userId
     * @return
     */
    public List<String> getUnPaiseList(String userId) {
        List<String> unPUid = new ArrayList<>();
        if (userShareList != null) {
            userShareList.stream().filter(o -> {
                JSONObject obj = (JSONObject) o;
                boolean[] flag = {true};
                obj.getJSONArray("praiseList").forEach(o1 -> {
                    JSONObject obj1 = (JSONObject) o1;
                    if (userId.equals(obj1.getString("userId"))) {
                        flag[0] = false;
                        return;
                    }
                });
                return flag[0];
            }).map(o2 -> {
                JSONObject obj2 = (JSONObject) o2;
                return obj2.getString("id");
            }).forEach(id -> unPUid.add(id));
        }
        return unPUid;
    }
}
